/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!		\file		PrpEdt.cc
		\date		june 2010
		\author		TNick

		\brief		Contains the implementation of EditorDel class and related


	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	Please read ReadMe.txt and Licence.txt in root folder
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include <QPainter>


#include "prpmdl.h"
#include "prpview.h"
#include "prpedt.h"
#include "prptpl.h"

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
EditorDel::EditorDel(QObject *parent)
	: QStyledItemDelegate(parent)
{
	pFth = static_cast<PrpView*>(parent);

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QWidget *EditorDel::createEditor(QWidget *parent,
								 const QStyleOptionViewItem &option,
								 const QModelIndex &index) const
{
	if (!index.isValid())
		return NULL;

	PrpTpl* theItem = static_cast<PrpTpl*>(index.internalPointer());
	if (theItem == NULL)
		return NULL;

	return theItem->editCreate(parent,option);
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void EditorDel::setEditorData(QWidget *editor,
							  const QModelIndex &index) const
{

	if (!index.isValid())
		return;

	PrpTpl* theItem = static_cast<PrpTpl*>(index.internalPointer());
	if (theItem == NULL)
		return;

	theItem->editLoad(editor);
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void EditorDel::setModelData(QWidget *editor, QAbstractItemModel *model,
							 const QModelIndex &index) const
{
	Q_UNUSED(model);
	if (!index.isValid())
		return;

	PrpTpl* theItem = static_cast<PrpTpl*>(index.internalPointer());
	if (theItem == NULL)
		return;

	theItem->editSave(editor);
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void EditorDel::updateEditorGeometry(QWidget *editor,
									 const QStyleOptionViewItem &option,
									 const QModelIndex &index) const
{

	if (!index.isValid())
		return;

	PrpTpl* theItem = static_cast<PrpTpl*>(index.internalPointer());
	if (theItem == NULL)
		return;

	theItem->editResize(editor, option);

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QSize EditorDel::sizeHint(const QStyleOptionViewItem &option,
						  const QModelIndex &index) const
{
	QSize s = QStyledItemDelegate::sizeHint(option, index);
	s.setHeight(s.height()*1.5);
	return s;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void EditorDel::paint(QPainter *painter,
					  const QStyleOptionViewItem &option,
					  const QModelIndex &index) const
{
	if (index.isValid())
		{
			PrpTpl* theItem =
					static_cast<PrpTpl*>(index.internalPointer());
			if (theItem != NULL)
				{
					if (theItem->type() == PrpTpl::HEADER)
						{
							QRect	atop = option.rect;
							/* 1.2 below is hackish, but decoration simply
							returns a value that is too samll */
							atop.setLeft(option.decorationSize.width()*1.2);
							atop.setWidth(
									pFth->columnWidth(0) +
									pFth->columnWidth(1) -
									atop.left());
							painter->save();
							QRadialGradient gradient(
									atop.left(),
									atop.top(), atop.width());
							gradient.setColorAt(0, QColor::fromRgb(0,150,0));
							gradient.setColorAt(1, QColor::fromRgb(255, 255, 255));

							QBrush brshd =
									QBrush(gradient);
							painter->setPen(Qt::NoPen);
							painter->setBrush(brshd);

							painter->drawRect(atop);

							QPen	pentx = QPen(QColor(255,255,255));
							pentx.setStyle(Qt::SolidLine);
							painter->setPen(pentx);
							atop.setWidth(atop.width()-atop.left());
							atop.setLeft(
									atop.left() * 2);
							QString qs = theItem->name();
							painter->drawText(
									atop,
									Qt::AlignLeft |
									Qt::AlignVCenter |
									Qt::TextSingleLine,
									qs);

							painter->restore();
							//QStyledItemDelegate::paint(painter,option,index);


							return;
						}
				}
		}
	QStyledItemDelegate::paint(painter,option,index);

}
/* ========================================================================= */

/*  CLASS    =============================================================== */
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
